package com.admin.system.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import com.admin.model.base.PageResult;
import com.admin.model.pojo.Dept;
import com.admin.model.dto.DeptDto;
import com.admin.monmon.util.AjaxResponse;
import com.admin.system.service.DeptService;
import com.admin.system.service.mapstruct.DeptMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Set;

/**
 * (Dept)表控制层
 *
 * @author makejava
 * @since 2021-01-31 20:16:25
 */
@Api(tags = "部门")
@RestController
@RequestMapping("dept")
public class DeptController {
    @Resource
    private DeptService deptService;

    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "children", "hasChildren", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('dept:add')")
    @PostMapping
    public ResponseEntity<Object> create(@Validated({DeptDto.Create.class, Default.class}) @RequestBody DeptDto deptDto) {
        Dept dept = deptService.create(deptDto);
        return ResponseEntity.ok(dept);
    }

    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('dept:list')")
    @GetMapping("/{id}")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return ResponseEntity.ok(deptService.findById(id));
    }

    @ApiOperation("查询部门树")
    @GetMapping("/tree")
    public ResponseEntity<Object> tree() {
        List<Tree<String>> trees = deptService.buildTree();
        return ResponseEntity.ok(trees);
    }

    @ApiOperation("查询部门员工树")
    @GetMapping("/duTree")
    public ResponseEntity<Object> duTree() {
        List<Tree<String>> trees = deptService.buildDuTree();
        return ResponseEntity.ok(trees);
    }

    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('dept:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteByIds(@RequestBody Set<Long> ids) {
        deptService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"children", "hasChildren", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('dept:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(defaultValue = "+sort") String sort,
                                          @RequestBody(required = false) DeptDto deptDto) {
        PageResult pageResult = deptService.queryAll(deptDto, pageNum, pageSize, sort);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "children", "hasChildren", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('dept:edit')")
    @PutMapping
    public ResponseEntity<Object> updateById(@Validated({DeptDto.Update.class, Default.class}) @RequestBody DeptDto deptDto) {
        Dept dept = deptService.updateById(deptDto);
        return ResponseEntity.ok(dept);
    }

    @ApiOperation("pid查询")
    @PreAuthorize("hasAnyAuthority('dept:list')")
    @GetMapping("/par/{pid}")
    public ResponseEntity<Object> findByPid(@PathVariable Long pid) {
        List<DeptDto> deptDtoList = deptService.queryByPid(pid);
        return ResponseEntity.ok(deptDtoList);
    }

}